package com.ruoyi.video.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.video.mapper.VideoMapper;
import com.ruoyi.video.pojo.VideoEntity;
import com.ruoyi.video.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: new java files header..
 *
 * @author 兰剑
 * @version 1.0
 * @date 2023/2/27 11:25
 */
@Service
public class VideoService {

    @Autowired
    private VideoMapper videoMapper;


    /**@Description: 添加课程信息
     * @param videoEntity
     * @return
     * @version v1.0
     * @author 兰剑
     * @date 2023/2/27 11:33
     */
    public Result addVideo(VideoEntity videoEntity) {
//        添加课程信息
        videoEntity.setCreateTime(new Date());
//        调用mapper映射接口完成添加信息
        videoMapper.insert(videoEntity);
//        添加成功，返回结果
        return new Result(true,"1","添加成功");
    }

    /**@Description: 查询列表数据
     * @param videoEntity
     * @return
     * @version v1.0
     * @author 兰剑
     * @date 2023/2/27 11:33
     */
    public Map<String, Object> list(VideoEntity videoEntity) {
        QueryWrapper<VideoEntity> queryWrapper=new QueryWrapper<>();
//        精准查询编号
        if (videoEntity.getVideoNumber()!=null && videoEntity.getVideoNumber()!=""){
            queryWrapper.eq("video_number",videoEntity.getVideoNumber());
        }
//        模糊查询课程信息
        if (videoEntity.getVideoContent()!=null && videoEntity.getVideoContent()!=""){
            queryWrapper.like(true,"video_content",videoEntity.getVideoContent());
        }
//        时间区间查询
        if (videoEntity.getMinTime()!=null){
            queryWrapper.ge("create_time",videoEntity.getMinTime());
        }
        if (videoEntity.getMaxTime()!=null){
            queryWrapper.le("create_time",videoEntity.getMaxTime());
        }
//        开启分页
        Page<VideoEntity> page=new Page<>(videoEntity.getPageNum(), videoEntity.getPageSize());
        if (videoEntity.getDel()==1){
            //        调用mapper映射接口开启分页
            Page<VideoEntity> videoList = videoMapper.selectPage(page, queryWrapper);
            List<VideoEntity> records = videoList.getRecords();
            long total = videoList.getTotal();
            //         创建map容器返回给前端
            Map map=new HashMap();
            map.put("videoList",records);
            map.put("total",total);
        }
        return null;
    }

    /**@Description: 修改视频信息
     * @param videoEntity
     * @return
     * @version v1.0
     * @author 兰剑
     * @date 2023/2/27 11:34
     */
    public Result updateVideo(VideoEntity videoEntity) {
        videoEntity.setUpdateTime(new Date());
//        调用映射接口完成视频信息修改
        videoMapper.updateById(videoEntity);
//        返回结果信息
        return new Result(true,"1","修改成功");
    }

    /**@Description: 单删除
     * @param vid
     * @return
     * @version v1.0
     * @author 兰剑
     * @date 2023/2/27 11:34
     */
    public Result delVideo(Integer vid) {
//        调用映射接口完成单删接口
        videoMapper.deleteById(vid);
//        返回结果
        return new Result(true,"1","删除成功");
    }

    /**@Description: 批量删除
     * @param vids
     * @return
     * @version v1.0
     * @author 兰剑
     * @date 2023/2/27 11:34
     */
    public Result delAllVideo(String vids) {
//        调用映射接口完成批量删除接口
        videoMapper.deleteById(vids);
//        返回结果
        return new Result(true,"1","删除成功");

    }

    /**@Description: 逻辑删除
     * @param videoEntity
     * @return
     * @version v1.0
     * @author 兰剑
     * @date 2023/2/27 11:37
     */
    public Result deleteVideo(VideoEntity videoEntity) {
        videoEntity.setUpdateTime(new Date());
        if (videoEntity.getDel()==1){
            videoEntity.setDel(2);
//            执行修改
            videoMapper.updateById(videoEntity);
        }
        return new Result(true,"1","删除成功");
    }

    /**@Description: 审核通过
     * @param videoEntity
     * @return
     * @version v1.0
     * @author 兰剑
     * @date 2023/2/27 16:26
     */
    public Result examineStatusUp(VideoEntity videoEntity) {
        videoEntity.setUpdateTime(new Date());
        if (videoEntity.getVideoStatus()==1){
            videoEntity.setVideoStatus(2);
//            执行修改
            videoMapper.updateById(videoEntity);
        }
        return new Result(true,"1","审核通过");
    }

    /**@Description: 审核驳回
     * @param videoEntity
     * @return
     * @version v1.0
     * @author 兰剑
     * @date 2023/2/27 16:35
     */
    public Result examineStatusEnd(VideoEntity videoEntity) {
        videoEntity.setUpdateTime(new Date());
        if (videoEntity.getVideoStatus()==1 && videoEntity.getVideoStatus()==2){
            videoEntity.setVideoStatus(0);
//            执行修改
            videoMapper.updateById(videoEntity);
        }
        return new Result(true,"1","已驳回");
    }
}
